﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BurningGhoul : Enemy
{
    protected override void Start()
    {
        base.Start();
        SwitchPoint();
    }

    protected override void Update()
    {
        base.Update();
        BeAttack(beAttack);
        if (Mathf.Abs(transform.position.x - targetPoint.position.x) < 0.1 && attackTransform == null)
            SwitchPoint();
        else if (attackTransform != null)
        {
            targetPoint = attackTransform;
            return;
        }
        Debug.Log("beAttack:"+beAttack);
    }

    //朝向目标移动
    public void MoveToTarget()
    {
        transform.position = Vector2.MoveTowards(transform.position, targetPoint.position, speed * Time.deltaTime);
        FilpDirection();
    }

    //选择较远的点
    public void SwitchPoint()
    {
        if (Mathf.Abs(leftPoint.position.x - transform.position.x) > Mathf.Abs(rightPoint.position.x - transform.position.x))
        {
            targetPoint = leftPoint;
        }
        else
        {
            targetPoint = rightPoint;
        }
        
    }

    //翻转方向
    public void FilpDirection()
    {
        if(targetPoint.position.x < transform.position.x)
        {
            transform.rotation = Quaternion.Euler(0f, 0f, 0f);
        }else
        {
            transform.rotation = Quaternion.Euler(0f, 180f, 0f);
        }
    }

    public void BeAttack(bool state)
    {
        Debug.Log("beAttack:" + state);
        anim.SetBool("beAttack", state);

        //判断Enemy在Player哪一侧
        if (state && !isDead)
        {
            if (playerPoint.position.x < transform.position.x)
            {
                rb.AddForce(new Vector2(5, 5));
            }
            else
            {
                rb.AddForce(new Vector2(-5, 5));
            }
        }

        if (!state && !isDead)
            MoveToTarget();
    }
}
